﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WROA.Models.Entitys;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
using WROA.Models.IServices;
using System.Web.Mvc;

namespace WROA.Models.Services
{
    public class FlowService : Services, IFlowService
    {
        /// <summary>
        /// 修改流程
        /// </summary>
        /// <param name="flow"></param>
        public void UpdateFlowData(Flow flow)
        {
            XPCollection xp = new XPCollection(typeof(Flow), new BinaryOperator("Oid", flow.Oid, BinaryOperatorType.Equal));
            foreach (Flow f in xp)
            {
                f.Fl_name = flow.Fl_name;
                f.Fl_remark = flow.Fl_remark;
                f.Flowtype = flow.Flowtype;
                f.Form = flow.Form;
                f.Save();
            }
        }

        /// <summary>
        /// 修改表单资料
        /// </summary>
        /// <param name="form"></param>
        public void UpdateFormData(FormData form)
        {
            XPCollection xp = new XPCollection(typeof(FormData), new BinaryOperator("Oid", form.Oid, BinaryOperatorType.Equal));
            foreach (FormData f in xp)
            {
                f.Fo_name = form.Fo_name;
                f.Fo_remark = form.Fo_remark;
                f.Flowtype = form.Flowtype;
                f.Fo_html = form.Fo_html;
                f.Save();
            }
        }

        /// <summary>
        /// 根据id返回一个流程对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Flow GetFlow(string name)
        {
            Flow flow = null;
            XPCollection xp = new XPCollection(typeof(Flow), new BinaryOperator("Fl_name", name, BinaryOperatorType.Equal));
            foreach (Flow f in xp)
            {
                flow = f;
            }
            return flow;
        }

        /// <summary>
        /// 返回所有流程资料
        /// </summary>
        /// <returns></returns>
        public XPCollection GetAllFlow()
        {
            XPCollection xp = new XPCollection(typeof(Flow));
            SortProperty sp = new SortProperty("Fl_date", DevExpress.Xpo.DB.SortingDirection.Descending);
            xp.Sorting.Add(sp);
            return xp;
        }

        /// <summary>
        /// 根据名称返回一个流程对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FlowType GetFlowType(string name)
        {
            FlowType flowType = null;
            XPCollection xp = new XPCollection(typeof(FlowType), new BinaryOperator("Ft_name", name, BinaryOperatorType.Equal));
            foreach (FlowType f in xp)
            {
                flowType = f;
            }
            return flowType;
        }

        /// <summary>
        /// 返回所有流程类型资料
        /// </summary>
        /// <returns></returns>
        public XPCollection GetAllFlowType()
        {
            XPCollection xp = new XPCollection(typeof(FlowType));
            return xp;
        }

        /// <summary>
        /// 返回所有流程类型资料List
        /// </summary>
        /// <returns></returns>
        public List<SelectListItem> GetAllFlowTypeList()
        {
            List<SelectListItem> result = new List<SelectListItem>();
            XPCollection xp = new XPCollection(typeof(FlowType));
            foreach (FlowType ft in xp)
            {
                result.Add(new SelectListItem() { Text = ft.Ft_name, Value = ft.Ft_name });
            }
            return result;
        }

        /// <summary>
        /// 根据id返回一个表单对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FormData GetForm(string name)
        {
            FormData form = null;
            XPCollection xp = new XPCollection(typeof(FormData), new BinaryOperator("Fo_name", name, BinaryOperatorType.Equal));
            foreach (FormData f in xp)
            {
                form = f;
            }
            return form;
        }

        /// <summary>
        /// 返回所有表单资料
        /// </summary>
        /// <returns></returns>
        public XPCollection GetAllForm()
        {
            XPCollection xp = new XPCollection(typeof(FormData));
            return xp;
        }

        /// <summary>
        /// 返回所有表单名称List
        /// </summary>
        /// <returns></returns>
        public List<SelectListItem> GetAllFormNameList()
        {
            List<SelectListItem> result = new List<SelectListItem>();
            XPCollection xp = new XPCollection(typeof(FormData));
            foreach (FormData f in xp)
            {
                result.Add(new SelectListItem() { Text = f.Fo_name, Value = f.Fo_name });
            }
            return result;
        }

        /// <summary>
        /// 删除流程之前判断该流程是否running中，如果running中则不可删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsRunning(string name)
        {
            bool result;
            FlowService fs = new FlowService();
            Flow flow = fs.GetFlow(name);
            XPCollection xp1 = new XPCollection(typeof(ApplyDocument), new BinaryOperator("Flow", flow, BinaryOperatorType.Equal));
            XPCollection xp2 = new XPCollection(typeof(ApplyMeeting), new BinaryOperator("Flow", flow, BinaryOperatorType.Equal));
            XPCollection xp3 = new XPCollection(typeof(ApplyCar), new BinaryOperator("Flow", flow, BinaryOperatorType.Equal));
            if (xp1.Count != 0 || xp2.Count != 0 || xp3.Count != 0)
            {
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 禁用流程
        /// </summary>
        /// <param name="id"></param>
        public void UpdateIsBanTrue(int id)
        {
            XPCollection xp = new XPCollection(typeof(Flow), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (Flow f in xp)
            {
                f.Fl_isban = "禁用";
                f.Save();
            }
        }

        /// <summary>
        /// 启用流程
        /// </summary>
        /// <param name="id"></param>
        public void UpdateIsBanFalse(int id)
        {
            XPCollection xp = new XPCollection(typeof(Flow), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (Flow f in xp)
            {
                f.Fl_isban = "启用";
                f.Save();
            }
        }

        /// <summary>
        /// 判断该步骤是否为流程最后一个步骤
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ProcedureIsLastStep(int id)
        {
            bool result = false;
            XPCollection xp = new XPCollection(typeof(Procedure), new BinaryOperator("Oid", id, BinaryOperatorType.Equal));
            foreach (Procedure p in xp)
            {
                if (p.Pr_isfinished == true)
                {
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// 根据流程查找流程第一个步骤
        /// </summary>
        /// <param name="flow"></param>
        /// <returns></returns>
        public Procedure GetFirstProcedure(Flow flow)
        {
            Procedure procedure = null;
            GroupOperator go = new GroupOperator();
            go.Operands.Add(new BinaryOperator("Flow", flow, BinaryOperatorType.Equal));
            go.Operands.Add(new BinaryOperator("Step_id", 1, BinaryOperatorType.Equal));
            XPCollection xp = new XPCollection(typeof(Procedure), go);
            foreach (Procedure p in xp)
            {
                procedure = p;
            }
            return procedure;
        }

        /// <summary>
        /// 根据流程类型名称返回流程列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList<SelectListItem> GetFlowNameList(string flowTypeName)
        {
            IList<SelectListItem> flowNameList = new List<SelectListItem>();
            FlowType flowType = null;
            XPCollection xpFlowType = new XPCollection(typeof(FlowType), new BinaryOperator("Ft_name", flowTypeName, BinaryOperatorType.Equal));
            foreach (FlowType ft in xpFlowType)
            {
                flowType = ft;
            }
            XPCollection xpFlow = new XPCollection(typeof(Flow), new BinaryOperator("Flowtype", flowType, BinaryOperatorType.Equal));
            foreach (Flow f in xpFlow)
            {
                flowNameList.Add(new SelectListItem() { Text = f.Fl_name, Value = f.Fl_name });
            }
            return flowNameList;
        }
    }
}